﻿using KTD.Web.BLL;
using KTD.Web.Models;
using KTD.Web.Models.VD;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net;
using System.Web.Mvc;
using XXS.Common.ControllerExt;

namespace KTD.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            if (Session["user"] == null)
            {
                return RedirectToAction("Login");
            }
            Member user = Session["user"] as Member;
            ViewBag.Name = user.name;
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Login(LoginD im)
        {
            var user = MF.MemberManager.Check(im);
            if (user != null)
            {
                Session["user"] = user;
                return this.JsonNet(new { success = true });
            }
            else
            {
                return this.JsonNet(new { success = false });
            }

        }


        public ActionResult ImagePre(string src)
        {
            ViewBag.Src = src;
            return View();
        }

        public ActionResult DashBoard()
        {
            return View();
        }

        [OutputCache(Duration = 60 * 60, VaryByParam = "*")]
        public ActionResult GetLocation(LocationD im)
        {

            try
            {
                var res = MF.HouseManager.GetLocation(im);
                
                return this.JsonNet(new { success = true, data = res }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult DaoRuNewHouse()
        {

            try
            {

                for (int i = 1; i <= 100; i++)
                {
                    var res = new WebClient().DownloadString(
                $"https://we.qsace.com/app/index.php?i=10&t=0&v=8.0.68&from=wxapp&c=entry&a=wxapp&do=getnewhouselist&state=we7sid-889426f54a395be45c185343b7c99335&m=weixinmao_house&sign=c0b7d7bc891cbdd5e88e65a13d2c4e75&cityid=1&uid=12749&page={i}&houseareaid=0&housepriceid=&housetype=0&bid=0&type=new&tab=&keyword=&fromtype=&ismap=0&distance=0&isnear=0&latlng=&isselect=0");

                    var model = JsonConvert.DeserializeObject<HouseRes>(res);

                    // JObject data = JsonConvert.DeserializeObject<JObject>(res);
                    foreach (var item in model.data)
                    {
                        item.uid = item.Id.Value;
                        MF.HouseManager.Create(item);
                    }
                }



                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }


        public ActionResult DaoRuSaleHouse()
        {

            try
            {

                for (int i = 1; i <= 20; i++)
                {
                    var res = new WebClient().DownloadString(
                $"https://we.qsace.com/app/index.php?i=10&t=0&v=8.0.68&from=wxapp&c=entry&a=wxapp&do=getlethouselist&state=we7sid-889426f54a395be45c185343b7c99335&m=weixinmao_house&sign=c0b7d7bc891cbdd5e88e65a13d2c4e75&cityid=1&page={i}&houseareaid=0&housepriceid=0&housetype=0&bid=0&letway=0&keyword=");

                    var model = JsonConvert.DeserializeObject<HouseRes>(res);

                    // JObject data = JsonConvert.DeserializeObject<JObject>(res);
                    foreach (var item in model.data)
                    {
                        MF.HouseManager.Create(item);
                    }
                }



                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }

        public ActionResult DaoRuBigHouse()
        {

            try
            {

                for (int i = 1; i <= 10; i++)
                {
                    var res = new WebClient().DownloadString(
                $"https://we.qsace.com/app/index.php?i=10&t=0&v=8.0.68&from=wxapp&c=entry&a=wxapp&do=getnewhouselist&state=we7sid-889426f54a395be45c185343b7c99335&m=weixinmao_house&sign=c0b7d7bc891cbdd5e88e65a13d2c4e75&cityid=1&uid=12749&page={i}&houseareaid=&housepriceid=&housetype=0&bid=0&type=dazong&tab=&keyword=&fromtype=&ismap=0&distance=0&isnear=0&latlng=&isselect=0");

                    var model = JsonConvert.DeserializeObject<HouseRes>(res);

                    // JObject data = JsonConvert.DeserializeObject<JObject>(res);
                    foreach (var item in model.data)
                    {
                        MF.HouseManager.Create(item);
                    }
                }



                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }

        public ActionResult DaoRu() {

            var im =new  HouseQD();
            //im.Channel = "new";
            
            var res = MF.HouseManager.GetAllListBy(im);
            try
            {
                foreach (var item in res)
                {
                    if (item.uid==0)
                    {
                        continue;
                    }
                    
                    var str = new WebClient().DownloadString(
             $"https://we.qsace.com/app/index.php?i=10&t=0&v=8.0.68&from=wxapp&c=entry&a=wxapp&do=getnewhousedetail&state=we7sid-91daba241e43f7c374d25b4ea8a1da44&m=weixinmao_house&sign=dbf2f71ece02d8211f0879ddd91ecc71&id={item.uid}&fpid=0&uid=12749&_page=index");


                    JObject data = JsonConvert.DeserializeObject<JObject>(str);
                    if (data["message"].ToString() == "success")
                    {
                        if (!string.IsNullOrEmpty(item.content))
                        {
                            continue;
                        }
                        item.content = data["data"]["list"]["content"].ToString();
                        //item.click = Convert.ToInt32(data["data"]["list"]["click"].ToString());
                        //item.clickstr = data["data"]["list"]["clickstr"].ToString();
                        MF.HouseManager.Update(item);
                    }
                    else
                    {
                        item.status =Models.Enum.Status.已下架;
                        MF.HouseManager.Update(item);
                    }
                   
                }
                
                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
          
        }


        public ActionResult DaoRuNews()
        {

            try
            {
                for (int j = 1; j <= 4; j++)
                {
                    for (int i = 1; i <= 10; i++)
                    {
                        var res = new WebClient().DownloadString(
                 "https://we.qsace.com/app/index.php?i=10&t=0&v=8.0.68&from=wxapp&c=entry&a=wxapp&do=getsecondlist&state=we7sid-01a1bb82391ad3e1a28f9fbcdb0a9631&m=weixinmao_house&sign=d1ea81656215caf10f3fb48ce4369be5&pid="+j+"&page="+i);

                        var data = JsonConvert.DeserializeObject<NewsRes>(res);
                        foreach (var item in data.data)
                        {
                            item.pid = j;
                            MF.NewsManager.Create(item);
                        }
                    }
                }
               

                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }


        public ActionResult DaoRuSaler()
        {

            try
            {
                
                for (int i = 1; i <= 100; i++)
                {
                    try
                    {
                        var res = new WebClient().DownloadString(
                $"https://we.qsace.com/app/index.php?i=10&t=0&v=8.0.68&from=wxapp&c=entry&a=wxapp&do=getplatlist&state=we7sid-8e4a2f6a780ecb30a113f49eb4295c2d&m=weixinmao_house&sign=d54dd7765d84f1d9b6582d17e08c92b8&id=&uid=12749&page={i}&type=sale&isjion=0&status=0");

                        var model = JsonConvert.DeserializeObject<SalerRes>(res);

                        // JObject data = JsonConvert.DeserializeObject<JObject>(res);
                        foreach (var item in model.data.data)
                        {
                            try
                            {
                                MF.SalerManager.Create(item);
                            }
                            catch
                            {
                                continue;
                            }
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
                


                return this.JsonNet(new { success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return this.JsonNet(new { success = false, msg = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }




    }
}